class CreateRoles < ActiveRecord::Migration
  def self.up
    create_table :roles do |t|
      t.column :lock_version, :integer,   :null => false
      t.column :name,         :string,    :null => false, :limit => 30
      t.column :initial,      :boolean,   :null => false, :default => false
      t.column :created_at,   :timestamp, :null => false
      t.column :updated_at,   :timestamp, :null => false
    end

    add_index :roles, :name, :unique => true

    create_table :authorities_roles, :id => false do |t|
      t.column :authority_id, :integer, :null => false
      t.column :role_id,      :integer, :null => false
    end

    add_index :authorities_roles, [:authority_id, :role_id], :unique => true

    admin = Role.create :name => "ROLE_ADMIN", :initial => false
    user  = Role.create :name => "ROLE_USER",  :initial => true

    auth_index = Authority.create :name => "AUTH_INDEX"

    admin.authorities << auth_index
    user.authorities  << auth_index
  end

  def self.down
    drop_table :authorities_roles
    drop_table :roles
  end
end
